package com.blueocean_health.care.management.appender;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.exception.OrganizationException;
import com.blueocean_health.care.management.exception.ProjectException;

/** 全局异常catch处理类--  处理500 */
@ControllerAdvice
public class GlobalExceptionHandler {
	
	@Value("${sys.debug}")
	private boolean debug;
	
	private Logger logger = Logger.getLogger(GlobalExceptionHandler.class); 
	
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public SysResult handlerException(Exception ex){
		logger.error("错误信息： "+ex.getMessage(),ex);
		System.out.println("错误信息： "+ex.getMessage());
		StackTraceElement[] stackTrace = ex.getStackTrace();
		for (StackTraceElement stackTraceElement : stackTrace) {
			System.out.println("------- "+stackTraceElement);
			//logger.error("------- "+stackTraceElement);
		}
		if(debug){
		ex.printStackTrace();
			return SysResult.error(ex.getMessage());
		}
		
		return SysResult.error();
	}
	
	@ExceptionHandler(OrganizationException.class)
	@ResponseBody
	public SysResult handleOrganizationException(OrganizationException ex) {
		return SysResult.error(ex.getMessage());
	}
	
	@ExceptionHandler(ProjectException.class)
	@ResponseBody
	public SysResult handleProjectException(ProjectException ex) {
		return SysResult.error(ex.getMessage());
	}
	
}
